package ch.supsi.mobile.core.applications;

import java.net.URL;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import ch.supsi.mobile.core.IPage;
import ch.supsi.mobile.core.Page;


public class BrowserApplication implements IBrowserApplication {
	
	DefaultListModel wModel = new DefaultListModel();
	DefaultListModel bModel = new DefaultListModel();
	DefaultListModel cModel = new DefaultListModel();
	
	JList whitePage = new JList(wModel);
	JList blackPage = new JList(bModel);
	
	public BrowserApplication(){
		
		rebuildList();
		
		wModel.addListDataListener(new ListDataListener() {
			
			@Override
			public void intervalRemoved(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void intervalAdded(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void contentsChanged(ListDataEvent arg0) {
				rebuildList();
			}
		});
		
		bModel.addListDataListener(new ListDataListener() {
			
			@Override
			public void intervalRemoved(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void intervalAdded(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void contentsChanged(ListDataEvent arg0) {
				rebuildList();
			}
		});
		
	}
	
	private void rebuildList(){
		cModel.removeAllElements();

		for(int i=0; i<wModel.size(); i++)
			cModel.addElement(wModel.get(i));
		for(int i=0; i<bModel.size(); i++)
			cModel.addElement(bModel.get(i));
	}
	
	public DefaultListModel getWhiteListModel(){
		return this.wModel;
	}
	
	public DefaultListModel getBlackListModel(){
		return this.bModel;
	}

	public DefaultListModel getCompleteListModel() {
		return cModel;
	}
	
	public IPage[] getPage(){
		return getWhitePage();
	}
	
	private IPage[] getWhitePage(){
		IPage[] listArray = new IPage[wModel.size()];
		
		for(int i=0; i<wModel.size(); i++)
			listArray[i] = (IPage)wModel.getElementAt(i);
		
		return listArray;
	}

	public IPage[] getBlackPage(){
		
		IPage[] listArray = new IPage[bModel.size()];
		
		for(int i=0; i<bModel.size(); i++)
			listArray[i] = (IPage)bModel.getElementAt(i);
		
		return listArray;
	}

	@Override
	public IPage getPage(URL url) {
		IPage[] list = getWhitePage();
		
		for(int i=0; i<list.length; i++){
			if(list[i].getURL().equals(url)){
				return list[i];
			}
		}

		list = getBlackPage();
		for(int i=0; i<list.length; i++){
			if(list[i].getURL().equals(url)){
				return list[i];
			}
		}
		
		return null;
	}
	
	public void addPage(IPage page){
		addWhitePage(page);
	}
	
	private void addWhitePage(IPage page) {
		//whitePage.add(page);
		wModel.addElement(page);
	}

	public void addBlackPage(IPage page) {
		//blackPage.add(page);
		bModel.addElement(page);
	}
	
	public void deletePage(IPage page){
		deleteWhitePage(page);
	}
	
	private void deleteWhitePage(IPage page) {
		//whitePage.remove(page);
		wModel.removeElement(page);
	}
	
	public void deleteBlackPage(IPage page) {
		//blackPage.remove(page);
		bModel.removeElement(page);
	}
}
